export default {
	name: "login",
	data() {
		const phone = (rule, value, callback) => {
			if (!this.validation.Phone(value)) {
				callback(new Error());
			}
			callback();
		};
		return {
			checked: false,
			isPhone: false,
			sysUser: {
				userName: "admin",
				password: "",
				phone: "",
				rememberMe: "off"
			},
			rules: {
				userName: [
					{required: true, message: "请输入账号", trigger: "blur"},
					{min: 1, max: 16, message: "长度在 1 到 16 个字符", trigger: "blur"}
				],
				phone: [
					{required: true, message: "请输入手机号码", trigger: "blur"},
					{validator: phone, message: "手机号码格式无效，为11位数字", trigger: "blur"}
				],
				password: [
					{required: true, message: "请输入密码", trigger: "blur"},
					{min: 1, max: 16, message: "长度在 1 到 16 个字符", trigger: "blur"}
				]
			}
		};
	},
	//页面加载调用获取cookie值
	mounted() {
		this.getCookie();
	},
	methods: {
		//登录
		login() {
			this.$refs.loginForm.validate(valid => {
				if (valid) {
					this.doLogin();
				} else {
					this.message.error("表单数据提交错误!");
					return false;
				}
			});
		},
		doLogin() {
			if (this.envMode !== "development" && this.sysUser.userName === "admin") {
				this.operateCookie();
				this.vuex("token", "123");
				this.vuex("user", {userName: this.sysUser.userName, permission: 6});
				this.initData(require("mock/menus.json"));
				this.message.success("登录成功!");
				this.$router.push({path: "/dashboard"});
			} else {
				this.doLogin4Net();
			}
			return true;
		},
		doLogin4Net() {
			this.fetch.login(this.sysUser,
				(res) => {
					if (res.headers.authorization) {
						this.operateCookie();
						this.vuex("token", res.headers.authorization);
						this.vuex("user", res.data.user);
						this.initData(res.data.menus);
						this.message.success("登录成功!");
						this.$router.push({path: decodeURIComponent(this.$route.params.redirect || "/dashboard")});
					}
				},
				(error) => {
					this.vuex("token", null);
					this.vuex("user", {});
					this.vuex("menus", []);
					this.clearCookie();//清空cookie
				});
		},
		initData(menus) {
			this.vuex("menus", this.filterMenus(menus));
			this.vuex("activeTabValue", "/dashboard");
			this.vuex("editableTabs", [{title: "dashboard", name: "/dashboard", icon: "el-icon-s-home", close: null}]);
			this.updateI18n();
		},
		updateI18n() {
			this.$i18n.mergeLocaleMessage("zh", this.menuHandler.createMenuCn());
			this.$i18n.mergeLocaleMessage("en", this.menuHandler.createMenuEn());
		},
		filterMenus(menuArray) {
			if (this.envMode !== "development") {
				return menuArray.filter(menu => menu.id.toString() !== "0");
			}
			return menuArray;
		},
		change() {
			this.isPhone = !this.isPhone;
			this.sysUser.password = "";
		},
		rememberMe() {
			this.sysUser.rememberMe = this.checked ? "on" : "off";
		},
		setLang(lang) {
			if (lang !== this.$i18n.locale) {
				this.$i18n.setup(lang);
				this.$i18n.locale = lang;
				this.vuex("locale", lang);
			}
		},
		//是否需要自动登录
		operateCookie() {
			//判断复选框是否被勾选 勾选则调用配置cookie方法
			if (this.checked === true) {
				let cookiePwd = this.cryptoUtils.encrypt(this.sysUser.password);
				let cookieName = this.cryptoUtils.encrypt(this.sysUser.userName);
				//传入账号名，密码
				this.setCookie(cookieName, cookiePwd);
			} else {//清空Cookie
				this.clearCookie();
			}
		},
		//设置cookie
		setCookie(cookieName, cookiePwd) {
			if (cookieName && cookiePwd) {
				//设置cookie有效期为1天,只在当前页面所在的路径有效；
				let expiresTimes = new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000);
				this.cookies.set("userName", cookieName, {expires: expiresTimes, path: "/", sameSite: "Lax"});
				this.cookies.set("password", cookiePwd, {expires: expiresTimes, path: "/", sameSite: "Lax", secure: true});
			} else {
				this.cookies.remove("userName", {path: "/"});
				this.cookies.remove("password", {path: "/"});
			}
		},
		//读取cookie
		getCookie() {
			if (this.cookies.get("userName") && this.cookies.get("password")) {
				//判断查找相对应的值
				this.sysUser.userName = this.cryptoUtils.decrypt(this.cookies.get("userName")); //保存到保存数据的地方
				this.sysUser.password = this.cryptoUtils.decrypt(this.cookies.get("password"));
				this.checked = true;
			}
		},
		//清除cookie,修改2值都为空
		clearCookie() {
			this.setCookie("", "");
		}
	}
};
